package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.StrategyCatalog;

import cn.wolfcode.wolf2w.business.mapper.StrategyCatalogMapper;
import cn.wolfcode.wolf2w.business.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.vo.CatalogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 攻略分类Service业务层处理
 * 
 * @author hyz
 * @date 2024-07-25
 */
@Service
@Transactional
public class StrategyCatalogServiceImpl extends ServiceImpl<StrategyCatalogMapper, StrategyCatalog> implements IStrategyCatalogService {

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyCatalog> queryPage(StrategyCatalogQuery qo) {
        IPage<StrategyCatalog> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyCatalog> queryByDestId(Long destId) {
        List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestId, destId)
                .list();
        for (StrategyCatalog strategyCatalog : list) {
            LambdaQueryWrapper<Strategy> qw = new LambdaQueryWrapper<>();
            qw.eq(Strategy::getCatalogId, strategyCatalog.getId());
            List<Strategy> strategies = strategyMapper.selectList(qw);
            strategyCatalog.setStrategies(strategies);
        }
        return list;
    }

    @Override
    public List<CatalogVO> queryCatalogVO() {
        List<StrategyCatalog> catalogs = query().select("distinct dest_name").list();
        ArrayList<CatalogVO> vos = new ArrayList<>();
        for (StrategyCatalog catalog : catalogs) {
            String destName = catalog.getDestName();
            List<StrategyCatalog> catalogList = lambdaQuery().eq(StrategyCatalog::getDestName, destName).list();
            CatalogVO vo = new CatalogVO(destName,catalogList);
            vos.add(vo);
        }
        return vos;
    }
}
